import { observable, action } from 'mobx';
import { WeaForm, WeaTableNew } from 'comsMobx';
import { WeaLocaleProvider } from 'ecCom';
import * as Api from '../../apis/setting/mailRule';
import { ValidateForm } from './util';

const getLabel = WeaLocaleProvider.getLabel;
const { TableStore } = WeaTableNew;
const initalState = {
  editVisible: false,
  editFields: [],
  isEdit: false,
  editLoading: false,
  editRule: [],
  editAction: [],
  editMailType: 0,
  ruleId: '',
  editTitle: '',
  actionconditioninfo: {},
  ruleconditioninfo_in: {},
  ruleconditioninfo_out: {},
  editType: '',
};
export default class RuleStore {
  @observable ruleTable = new TableStore();
  @observable editForm = new WeaForm();
  @observable editVisible = initalState.editVisible;
  @observable editFields = initalState.editFields;
  @observable isEdit = initalState.isEdit;
  @observable editLoading = initalState.editLoading;
  @observable editRule = initalState.editRule;
  @observable editAction = initalState.editAction;
  @observable editMailType = initalState.editMailType; // 内外部邮件 1内部 0外部
  @observable ruleId = initalState.ruleId;
  @observable editTitle = initalState.editTitle;
  @observable actionconditioninfo = initalState.actionconditioninfo;
  @observable ruleconditioninfo_in = initalState.ruleconditioninfo_in;
  @observable ruleconditioninfo_out = initalState.ruleconditioninfo_out;
  @observable editType = initalState.editType;

  @action setStoreState = (params) => {
    Object.keys(params).forEach((key) => {
      this[key] = params[key];
    });
  }

  @action clear = () => this.setStoreState(initalState);

  @action getEmailRuleList = (params, initPage = false) => {
    Api.getEmailRuleList(params).then((res) => {
      initPage ? this.ruleTable.getDatas(res.sessionkey) :
      this.ruleTable.getDatas(res.sessionkey, 1);
    });
  }

  @action operateUserEmailRule = async (params) => {
    let res = Api.operateUserEmailRule(params);
    return res;
  }

  @action getEmailRuleCondition = (params, callback) => {
    Api.getEmailRuleCondition(params).then((res) => {
      this.editForm = new WeaForm();
      this.editForm.initFormFields(res.conditioninfo);
      this.editFields = res.conditioninfo || [];
      if (callback) callback();
    });
  }

  @action editSubmit = (callback) => {
    ValidateForm(this.editForm, callback);
  }

  @action updateRule = (params) => {
    this.editLoading = true;
    this.editTitle = getLabel(84552, '编辑规则');
    this.isEdit = true;
    Api.getEmailRuleCondition(params).then((res) => {
      const { actionconditioninfo, conditioninfo, ruleconditioninfo_in, ruleconditioninfo_out } = res;
      this.editForm.resetForm();
      this.editForm = new WeaForm();
      this.editForm.setCondition(conditioninfo);
      this.editFields = conditioninfo;
      this.actionconditioninfo = actionconditioninfo.compDef || {};
      this.ruleconditioninfo_in = ruleconditioninfo_in.compDef || {};
      this.ruleconditioninfo_out = ruleconditioninfo_out.compDef || {};
      this.editMailType = this.editForm.getFormParams().mailType * 1;
      this.editLoading = false;
    });
    this.getEditRule(params);
  }

  @action getEditRule = (params, callback) => {
    Api.getUserEmailRuleEntity(params).then((res) => {
      const { emailRuleActionList, emailRuleConditionList } = res;
      this.editRule = emailRuleConditionList || [];
      this.editAction = emailRuleActionList || [];
      if (callback) callback();
    });
  }

  @action getEditCondition = (params, callback) => {
    this.editLoading = true;
    Api.getEmailRuleCondition(params).then((res) => {
      const { actionconditioninfo, conditioninfo, ruleconditioninfo_in, ruleconditioninfo_out } = res;
      this.editForm = new WeaForm();
      this.editForm.initFormFields(conditioninfo);
      this.editFields = conditioninfo;
      this.actionconditioninfo = actionconditioninfo.compDef || {};
      this.ruleconditioninfo_in = ruleconditioninfo_in.compDef || {};
      this.ruleconditioninfo_out = ruleconditioninfo_out.compDef || {};
      this.editMailType = this.editForm.getFormParams().mailType * 1;
      this.editLoading = false;
      this.getEditRule(params, callback);
    });
  }
}
